<template>
  <div class="addMenus">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="菜单名称">
        <el-input class="input" v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="上级菜单">
        <el-select v-model="form.pid" placeholder="请选择上级菜单">
          <el-option label="顶级菜单" :value="0"></el-option>
          <el-option label="系统设置" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="菜单类型">
        <el-radio-group v-model="form.type">
          <el-radio :label="1">目录</el-radio>
          <el-radio :label="2">菜单</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item class="input" label="菜单图标" v-show="form.type == 1">
        <el-input v-model="form.icon"></el-input>
      </el-form-item>
      <el-form-item class="input" label="菜单地址" v-show="form.type == 2">
        <el-input v-model="form.url"></el-input>
      </el-form-item>
      
      <el-switch
        class="mode"
        v-model="form.status"
        inactive-text="状态"
        :active-value="1" :inactive-value="2"
      >
      </el-switch>
      <el-form-item>
        <el-button type="primary" @click="finish">保存</el-button>
        <el-button type="info" @click="back">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {  getMenuApi,setMenuApi} from '@/api/menu'
export default {
  data() {
    return {
      form: {
        title: "",
        pid: "",
        type: "1",
        icon: "",
        status: 1,
        url: '', 
      },
    }
  },
  mounted(){
    this.getMenu()
  },
  methods: {
    //获取列表初始数据事件
    async getMenu() {
      let res=await getMenuApi(this.$route.query.id)
      this.form =res.list
      console.log(res);
    },
    //清除表单
    clearFormData () {
      this.form = {
        pid: '',
        title: '',
        icon: '',
        type: 1,
        url: '',
        status: 1
      }
    },
    async finish(){
        this.form.id=this.$route.query.id
        let res =await setMenuApi(this.form)
        if(res.code==200){
            alert(res.msg)
            this.$Bus.$emit('updata')
        }else{
            alert(res.msg)
        }
    },
    back(){
        this.$router.push('/menu')
    }
  },
};
</script>
<style scoped lang="less">
.addMenus {
  .input {
    width: 40%;
  }
  .mode{
    margin: 0 0 30px 12px;
  }
}
</style>
